<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!fullLocation">请选择配送地址</span>
      <span class="value" v-else>{{ fullLocation }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <div v-if="!showArea.length" class="loading"></div>
      <template v-else>
        <span
          class="ellipsis"
          v-for="city in showArea"
          :key="city.code"
          @click="handleArea(city)"
        >
          {{ city.name }}
        </span>
      </template>
    </div>
  </div>
</template>

<script>
import { computed, onBeforeMount, reactive, ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'

/* 中国地区数据 */
const getArea = () =>
  new Promise(resolve => {
    if (window.areaData) return resolve(window.areaData)
    const url =
      'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
    axios.get(url).then(({ data }) => resolve((window.areaData = data)))
  })

export default {
  name: 'XtxCity',
  setup () {
    // 控制展开收起,默认收起
    const active = ref(false)
    const areaData = ref([])
    const showArea = computed(() => {
      const { provinceName, cityName } = changeResult
      // 省列表
      if (!provinceName) {
        return areaData.value
      } else if (!cityName) {
        // 市列表
        return areaData.value.find(item => item.name === provinceName).areaList
      } else {
        // 区列表
        return areaData.value
          .find(item => item.name === provinceName)
          .areaList.find(item => item.name === cityName).areaList
      }
    })

    // 用户地区数据
    const changeResult = reactive({
      provinceName: '',
      cityName: '',
      countyName: ''
    })
    // 完整地址
    const fullLocation = computed(() => {
      const { provinceName, cityName, countyName } = changeResult
      return provinceName + cityName + countyName
    })

    onBeforeMount(async () => (areaData.value = await getArea()))

    const openDialog = async () => {
      active.value = true
      changeResult.provinceName = ''
      changeResult.cityName = ''
      changeResult.countyName = ''
    }

    const closeDialog = () => (active.value = false)

    // 切换展开收起
    const toggleDialog = () => {
      if (active.value) closeDialog()
      else openDialog()
    }
    // 点击其他位置隐藏
    const target = ref(null)
    onClickOutside(target, () => closeDialog())

    // 选中地区
    const handleArea = theItem => {
      const { level, name } = theItem
      if (level === 0) changeResult.provinceName = name

      if (level === 1) changeResult.cityName = name

      if (level === 2) {
        changeResult.countyName = name
        toggleDialog()
      }
    }

    return {
      active,
      toggleDialog,
      target,
      showArea,
      handleArea,
      areaData,
      fullLocation
    }
  }
}
</script>

<style scoped lang="less">
.option {
  // 省略...
  .loading {
    height: 290px;
    width: 100%;
    background: url(~@/assets/images/loading.gif) no-repeat center;
  }
}
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
